﻿namespace MetaVideoEditorEvolved.Converter
{
    using System;
    using System.Windows.Data;

    public class MinuteToHourConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return "Durée indéterminée";
            }

            TimeSpan hour = new TimeSpan(0, (int)value, 0);

            return hour.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var stringTime = value as string;

            if (string.IsNullOrEmpty(stringTime))
            {
                return null;
            }

            try
            {
                int hour = int.Parse(stringTime.Substring(0, 2));
                int min = int.Parse(stringTime.Substring(3, 2));
                int sec = int.Parse(stringTime.Substring(6, 2));

                var time = new TimeSpan(hour, min, sec);

                return time.TotalMinutes;
            }
            catch
            {
                return null;
            }
        }
    }
}
